unit uSearches;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShoppingServiceXMLClasses, StdCtrls, DB,
  EbayConnect, DBGridEh, ExtCtrls, DBCtrls,
  DBCtrlsEh, uEbaySearchThread, uTypes, 
  cxGraphics, cxControls, cxLookAndFeelPainters,
  cxContainer, cxDBEdit,  cxEdit, cxDropDownEdit, cxCalendar,
  cxGroupBox, cxRadioGroup, cxCheckBox, cxMemo, cxTextEdit, cxMaskEdit,
  cxSpinEdit,   cxDataStorage, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridCustomView,
  cxGridLevel, cxGrid, cxTL, cxTLdxBarBuiltInMenu,
  cxInplaceContainer,   cxDBLookupComboBox, dxtree,
  ImgList, ComCtrls, cxLookupEdit, cxDBLookupEdit, cxClasses, cxCheckGroup,
  cxDBCheckGroup, cxGridDBTableView, nxdb,  cxDBTL,
  cxTLData, cxLabel, cxLookAndFeels, cxStyles, cxCustomData, cxFilter, cxData,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinFoggy, dxSkinGlassOceans,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSharp, dxSkinSilver, dxSkinSpringTime,
  dxSkinStardust, dxSkinSummer2008, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinXmas2008Blue, dxSkinscxPCPainter;

type

  TfmSearches = class(TForm)
    dsSearches: TDataSource;
    Label1: TLabel;
    Label2: TLabel;
    il1: TImageList;
    dbtxtCategoryName: TDBText;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl5: TLabel;
    lbl6: TLabel;
    lbl7: TLabel;
    lbl8: TLabel;
    lbl13: TLabel;
    lbl15: TLabel;
    lbl18: TLabel;
    lbl19: TLabel;
    lbl20: TLabel;
    lbl21: TLabel;
    lbl22: TLabel;
    bvl2: TBevel;
    lbl23: TLabel;
    lbl24: TLabel;
    lbl25: TLabel;
    dsCountries: TDataSource;
    lbl26: TLabel;
    cxDBSpinEdit1: TcxDBSpinEdit;
    cxDBSpinEdit2: TcxDBSpinEdit;
    cxDBSpinEdit3: TcxDBSpinEdit;
    cxDBSpinEdit4: TcxDBSpinEdit;
    cxDBSpinEdit5: TcxDBSpinEdit;
    cxDBSpinEdit6: TcxDBSpinEdit;
    cxDBSpinEdit7: TcxDBSpinEdit;
    cxDBSpinEdit8: TcxDBSpinEdit;
    cxDBComboBox1: TcxDBComboBox;
    cxDBComboBox2: TcxDBComboBox;
    cxDBComboBox3: TcxDBComboBox;
    cxDBMemo1: TcxDBMemo;
    cxDBMemo2: TcxDBMemo;
    cxDBTextEdit1: TcxDBTextEdit;
    cxDBComboBox4: TcxDBComboBox;
    cxDBComboBox5: TcxDBComboBox;
    cxDBCheckBox1: TcxDBCheckBox;
    cxDBCheckBox2: TcxDBCheckBox;
    cxDBCheckBox3: TcxDBCheckBox;
    cxDBCheckBox4: TcxDBCheckBox;
    cxDBCheckBox5: TcxDBCheckBox;
    cxDBCheckBox6: TcxDBCheckBox;
    cxDBCheckBox7: TcxDBCheckBox;
    cxDBCheckBox8: TcxDBCheckBox;
    cxDBCheckBox9: TcxDBCheckBox;
    cxDBCheckBox10: TcxDBCheckBox;
    cxDBDateEdit1: TcxDBDateEdit;
    lbl9: TLabel;
    cxDBDateEdit2: TcxDBDateEdit;
    cxDBSpinEdit9: TcxDBSpinEdit;
    cxDBTextEdit2: TcxDBTextEdit;
    cxDBTextEdit3: TcxDBTextEdit;
    dsCategories: TDataSource;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    cxDBLookupComboBox2: TcxDBLookupComboBox;
    lbl3: TLabel;
    lbl4: TLabel;
    cxDBLookupComboBox3: TcxDBLookupComboBox;
    cxDBCheckGroup1: TcxDBCheckGroup;
    Label3: TLabel;
    cbb1: TcxDBComboBox;
    cxDBMemo3: TcxDBMemo;
    lbl10: TLabel;
    lbl11: TLabel;
    Label4: TLabel;
    cxdbchckbx1: TcxDBCheckBox;
    cxdbchckbx2: TcxDBCheckBox;
    lbl12: TLabel;
    lbl14: TLabel;
    bvl1: TBevel;
    btn1: TButton;
    dsaspects: TDataSource;
    dsaspectvalues: TDataSource;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGrid1TableView1: TcxGridTableView;
    cxgrdbtblvwGrid1DBTableView1: TcxGridDBTableView;
    cxgrdlvlGrid1Level2: TcxGridLevel;
    cxgrdbtblvwGrid1DBTableView2: TcxGridDBTableView;
    cxgrdbclmnGrid1DBTableView1aspect: TcxGridDBColumn;
    cxgrdbclmnGrid1DBTableView2aspectvalname: TcxGridDBColumn;
    cxgrdbclmnGrid1DBTableView2count: TcxGridDBColumn;
    cxgrdbclmnGrid1DBTableView2selected: TcxGridDBColumn;
    tblaspectvalues: TnxTable;
    IntegerField1: TIntegerField;
    StringField1: TStringField;
    IntegerField2: TIntegerField;
    tblaspectvaluesselected: TBooleanField;
    tblaspects: TnxTable;
    IntegerField3: TIntegerField;
    StringField2: TStringField;
    tblCountries: TnxTable;
    qryCategories: TnxQuery;
    qryCategoriesChildrens: TIntegerField;
    qryCategoriesCategoryName: TStringField;
    qryCategoriesLeafCategory: TBooleanField;
    qryCategoriesCategoryID: TIntegerField;
    qryCategoriesCategoryParentID: TIntegerField;
    qryCategoriesCategoryLevel: TIntegerField;
    tblSearchesenabled: TBooleanField;
    tblSearchesname: TStringField;
    tblSearcheskeywords: TStringField;
    tblSearchesDescriptionSearch: TBooleanField;
    tblSearchesCategoryID: TStringField;
    tblSearchesCategoryName: TStringField;
    tblSearchesAvailableTo: TStringField;
    tblSearchesCondition: TStringField;
    tblSearchesCurrency: TStringField;
    tblSearchesEndTimeFrom: TDateTimeField;
    tblSearchesEndTimeTo: TDateTimeField;
    tblSearchesExcludeAutoPay: TBooleanField;
    tblSearchesExcludeSeller: TStringField;
    tblSearchesFeaturedOnly: TBooleanField;
    tblSearchesFeedbackScoreMax: TIntegerField;
    tblSearchesFeedbackScoreMin: TIntegerField;
    tblSearchesFreeShippingOnly: TBooleanField;
    tblSearchesGetItFastOnly: TBooleanField;
    tblSearchesHideDuplicateItems: TBooleanField;
    tblSearchesListingType: TStringField;
    tblSearchesLocalPickupOnly: TBooleanField;
    tblSearchesLocalSearchOnly: TBooleanField;
    tblSearchesLocatedIn: TStringField;
    tblSearchesLotsOnly: TBooleanField;
    tblSearchesMaxBids: TIntegerField;
    tblSearchesMaxDistance: TIntegerField;
    tblSearchesMaxPrice: TFloatField;
    tblSearchesMaxQuantity: TIntegerField;
    tblSearchesMinBids: TIntegerField;
    tblSearchesMinPrice: TFloatField;
    tblSearchesMinMaxPriceCurrency: TStringField;
    tblSearchesMinQuantity: TIntegerField;
    tblSearchesPaymentMethod: TStringField;
    tblSearchesSeller: TStringField;
    tblSearchesSellerBusinessType: TStringField;
    tblSearchesTopRatedSellerOnly: TBooleanField;
    tblSearchesWorldOfGoodOnly: TBooleanField;
    tblSearchesBuyerPostalCode: TStringField;
    tblSearchesGlobalSiteID: TStringField;
    tblSearchesSortOrder: TStringField;
    tblSearchesApiCallsUsedToday: TIntegerField;
    tblSearchesSearchPeriod: TIntegerField;
    tblSearchesLastSearchStarted: TDateTimeField;
    tblSearchesAutoExpandLeafCategories: TBooleanField;
    tblSearches: TnxTable;
    cxDBCheckBox11: TcxDBCheckBox;
    pnl1: TPanel;
    btn2: TButton;
    btn3: TButton;
    tblCountriesName: TStringField;
    tblCountriesCode: TStringField;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn1: TcxDBTreeListColumn;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    qryCategoriesileaf: TSmallintField;
    tblaspectvaluesid: TAutoIncField;
    tblaspectsid: TAutoIncField;
    tblSearchesid: TAutoIncField;
    tblCountriesid: TAutoIncField;
    qryCategoriesGlobalSiteID: TStringField;
    pnl2: TPanel;
    lbl17: TLabel;
    lbl27: TLabel;
    
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure qryCategoriesCalcFields(DataSet: TDataSet);
    procedure tblSearchesAfterInsert(DataSet: TDataSet);
    procedure tv1DblClick(Sender: TObject);
    procedure tblSearchesAvailableToChange(Sender: TField);
    procedure tblSearchesLocatedInChange(Sender: TField);
    procedure cxDBMemo3KeyPress(Sender: TObject; var Key: Char);
    procedure btn1Click(Sender: TObject);
    procedure OnFindAdvThreadTerminate(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure qryCategoriesAfterOpen(DataSet: TDataSet);
    function FillSearchRecord(var SearchProperties :TSearchProperties):boolean;
    procedure cxDBLookupComboBox3PropertiesChange(Sender: TObject);
    procedure cxDBCheckGroup1PropertiesChange(Sender: TObject);


  private
    { Private declarations }
    Aspectthread : TFindAdvThread;
  public
    { Public declarations }
  end;

var
  fmSearches: TfmSearches;

implementation

uses TypInfo, FindingServiceXMLClasses, uMain, HotLog, CommonTypes;
{$R *.dfm}

procedure TfmSearches.FormShow(Sender: TObject);
begin
  if not tblSearches.Active then tblSearches.Open;
  //if not qryCategories.Active then qryCategories.Open;
  if not tblCountries.Active then tblCountries.Open;
  if not fmMain.qryActiveSites.Active then fmMain.qryActiveSites.Open;
  if not tblaspects.Active then tblaspects.Open;
  if not tblaspectvalues.Active then tblaspectvalues.Open;
end;

procedure TfmSearches.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if tblSearches.Active then tblSearches.Close;
  if qryCategories.Active then qryCategories.Close;
  if tblCountries.Active then tblCountries.Close;
  Action := caFree;
  fmSearches := nil;
end;

procedure TfmSearches.FormCreate(Sender: TObject);
var i : Integer;
begin
  cxDBCheckGroup1.Properties.Items.Clear;
  for i:= 0 to Ord(High(CurrencyCodeType)) do cxDBComboBox5.Properties.Items.Add(GetEnumName(TypeInfo(CurrencyCodeType),ord(i)));
  for i:= 0 to Ord(High(CurrencyCodeType)) do cxDBComboBox1.Properties.Items.Add(GetEnumName(TypeInfo(CurrencyCodeType),ord(i)));
  for i:= 0 to Ord(High(ListingTypes)) do  with cxDBCheckGroup1.Properties.Items.Add do Caption := GetEnumName(TypeInfo(ListingTypes),ord(i));
  tblSearches.Database := fmMain.nxdtbs1;
  tblaspectvalues.Database := fmMain.nxdtbs1;
  tblaspects.Database := fmMain.nxdtbs1;
  tblCountries.Database := fmMain.nxdtbs1;
  qryCategories.Database := fmMain.nxdtbs1;
  tblSearches.MasterSource := fmMain.dsAllSeraches;
end;

procedure TfmSearches.qryCategoriesAfterOpen(DataSet: TDataSet);
begin
  if tblSearches.Active then
  if not tblSearches.FieldByName('CategoryID').IsNull then
  if tblSearches.FieldByName('CategoryID').Value <> '' then
  qryCategories.Locate('CategoryID',tblSearches.FieldByName('CategoryID').Value,[]);
end;

procedure TfmSearches.qryCategoriesCalcFields(DataSet: TDataSet);
begin
  if qryCategories.FieldByName('LeafCategory').Value then  qryCategories.FieldByName('Childrens').Value := 0
  else qryCategories.FieldByName('Childrens').Value := 1;
  if qryCategories.FieldByName('LeafCategory').Value then  qryCategories.FieldByName('ileaf').Value :=1
  else qryCategories.FieldByName('ileaf').Value :=0;
end;

procedure TfmSearches.tblSearchesAfterInsert(DataSet: TDataSet);
begin
  tblSearches.FieldByName('GlobalSiteID').Value := 'EBAY_US';
  tblSearches.FieldByName('enabled').Value := true;
end;

procedure TfmSearches.tv1DblClick(Sender: TObject);
begin
  //if tv1.DBSelected.HasChildren then Exit;
  if dsSearches.State in [dsBrowse] then tblSearches.Edit;
  tblSearches.FieldByName('CategoryID').Value := qryCategories.FieldByName('CategoryID').Value;
  tblSearches.FieldByName('CategoryName').Value := qryCategories.FieldByName('CategoryName').Value;
  //tblSearches.Post;
end;

procedure TfmSearches.tblSearchesAvailableToChange(Sender: TField);
begin
  cxDBLookupComboBox2.Enabled := tblSearches.FieldByName('AvailableTo').IsNull;
end;

procedure TfmSearches.tblSearchesLocatedInChange(Sender: TField);
begin
  cxDBLookupComboBox1.Enabled := tblSearches.FieldByName('LocatedIn').IsNull;
end;

procedure TfmSearches.btn2Click(Sender: TObject);
begin
  if dsSearches.State in [dsEdit,dsInsert] then tblSearches.Post;
end;

procedure TfmSearches.btn3Click(Sender: TObject);
begin
  if dsSearches.State in [dsEdit,dsInsert] then tblSearches.Cancel;
end;

procedure TfmSearches.cxDBMemo3KeyPress(Sender: TObject; var Key: Char);
begin
  //MessageDlg('changes',mtInformation,[mbOK],0);
end;

procedure TfmSearches.btn1Click(Sender: TObject);
var SearchProperties : TSearchProperties;
begin
  // load aspects
  cxGrid1.Enabled := False;
  btn1.Enabled := False;
  Aspectthread := TFindAdvThread.Create(fmSearches);
  with Aspectthread do begin
    OnTerminate := OnFindAdvThreadTerminate;
    AppID := fmMain.inisettings.AppID;
    OutputSelector := [AspectHistogram,CategoryHistogram];
    if FillSearchRecord(SearchProperties) then begin
      fmMain.SetSearchPropertiesFromRecord(Aspectthread, fmMain.IniSettings , SearchProperties, true);
      fmMain.CurrentOperation := opLoadAspects;
      PBasePath := fmMain.BasePath;
      XMLDebugOutPut := true;
      FetchAllPages :=False;
      IgnoreCategories :=True ;
      ItemsPerPage := 1;
      SaveAspectValues := True;
      SearchID := tblSearches.FieldByName('id').Value;
      Start;
    end
    else Aspectthread.Free;
  end;
end;

procedure TfmSearches.OnFindAdvThreadTerminate(Sender: TObject);
begin
  fmMain.CurrentOperation := opIdle;
  tblaspects.Close;
  tblaspects.Open;
  tblaspectvalues.Close;
  tblaspectvalues.Open;
  cxGrid1.Enabled := True;
  btn1.Enabled := True;
  cxgrdbtblvwGrid1DBTableView1.DataController.UpdateData;
  cxgrdbtblvwGrid1DBTableView2.DataController.UpdateData;
end;

function TfmSearches.FillSearchRecord(var SearchProperties: TSearchProperties):Boolean;
begin
  Result := False;
  with SearchProperties do begin
    // reset record
    rGlobalID :='';
    rCategoryID :='';
    rBuyerPostalCode :='';
    rKeyWords :='';
    rDescriptionSearch :=False;
    rCondition :='';
    rSearchID :=0;
    rLocatedIn :='';
    rAvailableto :='';
    rSeller :='';
    rExcludeSeller :='';
    rListingType :='';
    rPriceFrom :=0;
    rPriceTo :=0;
    rPriceCurrency :='';
    rFeedbackScoreMax :=0;
    rFeedbackScoreMin :=0;
    rBidsMax :=0;
    rBidsMin :=0;
    rQuantityMax :=0;
    rQuantityMin :=0;
    rEndTimeFrom :=0;
    rEndTimeTo :=0;
    rMaxDistance :=0;
    rLocalSearchOnly :=False;
    rPaymentMethod :='';
    rSellerBusinessType :='';
    rCurrency :='';
    rHideDuplicates :=False;
    rSortOrder :='';
    // assign values
    if cxDBLookupComboBox3.EditValue<>null then rGlobalID := cxDBLookupComboBox3.EditValue
    else Exit;
    if cxDBTextEdit2.EditValue<>null then rCategoryID := cxDBTextEdit2.EditValue;
    if cxDBMemo3.EditValue<>null then rKeyWords := cxDBMemo3.EditValue;
    if (Length(rKeyWords)=0) and (Length(rCategoryID)=0) then exit;
    Result := True;
    if cxDBTextEdit1.EditValue<>null then rBuyerPostalCode := cxDBTextEdit1.EditValue;
    rDescriptionSearch := cxdbchckbx2.Checked;
    if cxDBComboBox4.EditValue<>null then rCondition := cxDBComboBox4.EditValue;
    if cxDBLookupComboBox2.EditValue<>null then rLocatedIn := cxDBLookupComboBox2.EditValue;
    rSearchID := tblSearches.FieldByName('id').Value;
    if cxDBLookupComboBox1.EditValue<>null then rAvailableto := cxDBLookupComboBox1.EditValue;
    if cxDBMemo1.EditValue<>null then rSeller := cxDBMemo1.EditValue;
    if cxDBMemo2.EditValue<>null then rExcludeSeller := cxDBMemo2.EditValue;
    if cxDBCheckGroup1.EditValue<>null then rListingType := cxDBCheckGroup1.EditValue;
    if cxDBSpinEdit4.EditValue<>null then rPriceFrom := cxDBSpinEdit4.EditValue;
    if cxDBSpinEdit3.EditValue<>null then rPriceTo := cxDBSpinEdit3.EditValue;
    if cxDBComboBox1.EditValue<>null then rPriceCurrency := cxDBComboBox1.EditValue;
    if cxDBSpinEdit2.EditValue<>null then rFeedbackScoreMin := cxDBSpinEdit2.EditValue;
    if cxDBSpinEdit1.EditValue<>null then rFeedbackScoreMax := cxDBSpinEdit1.EditValue;
    if cxDBSpinEdit6.EditValue<>null then rBidsMin := cxDBSpinEdit6.EditValue;
    if cxDBSpinEdit5.EditValue<>null then rBidsMax := cxDBSpinEdit5.EditValue;
    if cxDBSpinEdit8.EditValue<>null then rQuantityMin := cxDBSpinEdit8.EditValue;
    if cxDBSpinEdit7.EditValue<>null then rQuantityMax := cxDBSpinEdit7.EditValue;
    if cxDBDateEdit1.EditValue<>null then rEndTimeFrom := cxDBDateEdit1.EditValue;
    if cxDBDateEdit2.EditValue<>null then rEndTimeTo := cxDBDateEdit2.EditValue;
    if cxDBSpinEdit9.EditValue<>null then rMaxDistance := cxDBSpinEdit9.EditValue;
    if cxDBComboBox2.EditValue<>null then rPaymentMethod := cxDBComboBox2.EditValue;
    if cxDBComboBox3.EditValue<>null then rSellerBusinessType := cxDBComboBox3.EditValue;
    if cxDBComboBox5.EditValue<>null then rCurrency := cxDBComboBox5.EditValue;
    if cbb1.EditValue<>null then rSortOrder := cbb1.EditValue;
    rLocalSearchOnly := cxDBCheckBox10.Checked;
    rHideDuplicates :=cxDBCheckBox6.Checked;
  end;
end;

procedure TfmSearches.cxDBLookupComboBox3PropertiesChange(Sender: TObject);
var ebaymotor: Boolean;
begin
  if qryCategories.ParamByName('GlobalSiteID').AsString <> cxDBLookupComboBox3.EditValue then begin
    if tblSearches.FieldByName('GlobalSiteID').Value <> cxDBLookupComboBox3.EditValue then begin
       if dsSearches.State in [dsBrowse] then tblSearches.Edit;
       tblSearches.FieldByName('CategoryID').Value :='';
       tblSearches.FieldByName('CategoryName').Value :='';
    end;
    qryCategories.ParamByName('GlobalSiteID').AsString := cxDBLookupComboBox3.EditValue;
    if tblSearches.Active then begin
      qryCategories.Close;
      qryCategories.Open;
    end;
  end;
  ebaymotor := cxDBLookupComboBox3.EditValue = 'EBAY_MOTOR';
  cxDBCheckBox10.Enabled := ebaymotor;
  cxDBSpinEdit9.Enabled := ebaymotor;
  lbl13.Enabled := ebaymotor;
end;

procedure TfmSearches.cxDBCheckGroup1PropertiesChange(Sender: TObject);
var str : string;
    i : Integer;
begin

  str := cxDBCheckGroup1.Properties.Items.Items[cxDBCheckGroup1.Properties.Items.Count-1].Caption;
  str := IntToStr(Length(str))+':'+str;
  if Pos(str,cxDBCheckGroup1.EditValue)>0 then begin
    for i:= 0 to cxDBCheckGroup1.Properties.Items.Count-2 do cxDBCheckGroup1.Properties.Items.Items[i].Enabled := False;
    cxDBCheckGroup1.EditValue := ';'+str;
  end
  else begin
    for i:= 0 to cxDBCheckGroup1.Properties.Items.Count-2 do cxDBCheckGroup1.Properties.Items.Items[i].Enabled := True;
    //cxDBCheckGroup1.EditValue := '';
  end;
  MessageDlg(cxDBCheckGroup1.EditValue,mtInformation,[mbOK],0);
end;

end.

{

 <?xml version="1.0" encoding="UTF-8"?>
<findItemsIneBayStoresRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">
  <keywords>harry potter</keywords>
  <storeName>Laura_Chen's_Small_Store</storeName>
  <outputSelector>StoreInfo</outputSelector>
  <itemFilter>
    <name>MinPrice</name>
    <value>11.00</value>
    <paramName>Currency</paramName>
    <paramValue>USD</paramValue>
  </itemFilter>
  <itemFilter>
    <name>MaxPrice</name>
    <value>25.00</value>
    <paramName>Currency</paramName>
    <paramValue>USD</paramValue>
  </itemFilter>
  <paginationInput>
    <entriesPerPage>1</entriesPerPage>
  </paginationInput>
</findItemsIneBayStoresRequest>

<?xml version="1.0" encoding="UTF-8"?>
<findItemsAdvancedRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">
  <categoryId>31388</categoryId>
  <outputSelector>AspectHistogram</outputSelector>
  <itemFilter>
    <name>Condition</name>
    <value>Used</value>
  </itemFilter>
  <itemFilter>
    <name>ListingType</name>
    <value>AuctionWithBIN</value>
  </itemFilter>
  <paginationInput>
    <entriesPerPage>2</entriesPerPage>
  </paginationInput>
</findItemsAdvancedRequest>

SELECT * FROM "Categories"
where GlobalSiteID= 'EBAY_US'

SELECT CategoryID, CategoryLevel, CategoryName, CategoryParentID,  LeafCategory, GlobalSiteID
FROM  "Categories"
where GlobalSiteID = :GlobalSiteID
order by CategoryParentID , CategoryName


}
